import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class InnerController extends JFrame{
    private JLabel labContainer;
    private JLabel lab;             // 各楼层数字-1,2,3...
    private JLabel labDisplay;      // 显示牌
    private JLabel labFloor;        // 显示牌-楼层
    private JLabel labStatus;       // 显示牌-状态（上或下）
    private JLabel labCountPeople;  // 显示电梯内人数
    private JLabel timeDisplay;     //显示系统时间
    private JTextArea labCmd;       // 显示随机内呼命令

    private JButton btnCmd;         // 产生随机内呼命令的按钮
    private JButton repairJbt;      //电梯维修按钮

    private int elevatorId;         // 电梯id
    protected  OuterController outter;  //调用外呼控制器

    public InnerController(int elevatorId) {
        this.elevatorId = elevatorId;
        outter = OuterController.getInstance();
        init();
        new Thread(outter.elevator[elevatorId]).start();    //启动电梯线程
    }

    private void init() {
        // 窗口设置
        setTitle(elevatorId + "号电梯");
        setBounds(100,20,750,700);
        setLayout(null);
        setResizable(false);
        validate();

        Container con=this.getContentPane();
        labContainer = new JLabel();
        con.add(labContainer);
        labContainer.setBounds(0,0,900,700);

        //显示楼层信息
        for(int i=0; i<20; i++) {
            lab = new JLabel(i+1+""+"层");
            lab.setBounds(10, 650-30*i, 50, 20);
            lab.setBorder(BorderFactory.createLineBorder(Color.black));
            labContainer.add(lab);
        }
        lab = new JLabel("   楼层  ");
        lab.setBounds(10, 50, 50, 20);
        labContainer.add(lab);

        //画电梯
        for(int j=0; j<20; j++) {
            outter.elevator[elevatorId].elevatorPanel[j] = new JLabel();
            outter.elevator[elevatorId].elevatorPanel[j].setBounds(80,650-30*j,70,20);
            outter.elevator[elevatorId].elevatorPanel[j].setBorder(BorderFactory.createLineBorder(Color.black));
            labContainer.add(outter.elevator[elevatorId].elevatorPanel[j]);
        }
        outter.elevator[elevatorId].elevatorPanel[0].setOpaque(true);
        outter.elevator[elevatorId].elevatorPanel[0].setBackground(Color.BLACK);

        //outter.elevator[1].getOrderMap().put(7,4);
        //System.out.println(outter.elevator[1].getRandomCmd().size());

        //画电梯旁的外呼按钮
        /*for(int k=0; k<40; k++){
            if(k % 2 == 0) {
                outter.directionButton[k] = new JButton("上");
                outter.directionButton[k].setBackground(Color.WHITE);
                outter.directionButton[k].setBounds(180, 650 - 30 * k, 50, 20);
                labContainer.add(outter.directionButton[k]);
            }
            else{
                outter.directionButton[k] = new JButton("下");
                outter.directionButton[k].setBackground(Color.WHITE);
                outter.directionButton[k].setBounds(230, 650-30 * (k-1), 50, 20);
                labContainer.add(outter.directionButton[k]);
            }
            JButton temp = outter.directionButton[k];
            int tempFloor = k + 1;
            outter.directionButton[k].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    temp.setBackground(Color.YELLOW);
                    HElevator h = outter.elevator[elevatorId];
                    h.setRandomCmd(tempFloor, 2);
                    if(h.getDirection() == HElevator.PAUSED){
                        h.setLastCmd(tempFloor);    //如果电梯是空闲状态，把楼层按键的值设为最终目标楼层
                    }

                    if(h.getDirection() == HElevator.UP){
                        h.setLastCmd(h.getMaxCmd());
                    }

                    if(h.getDirection() == HElevator.DOWN){
                        h.setLastCmd(h.getMinCmd());
                    }
                }
            });


        }**/
        //将外呼按钮组加入面板
        for(int k = 0; k < 40; k++){
            if(k == 1 || k == 38)
                continue;           //一层和顶层分别不设置向下和向上按钮
            JButton outterJbt = outter.directionButton[k];
            labContainer.add(outterJbt);
        }

        //将维修按钮加入面板
        repairJbt = new JButton("修理电梯");
        repairJbt.setBounds(550,500,100,40);
        repairJbt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(repairJbt.getText().equals("修理电梯")) {
                    if(outter.elevator[elevatorId].getDirection() == HElevator.PAUSED){
                        outter.elevator[elevatorId].setState(HElevator.REPAIR);
                        repairJbt.setText("恢复电梯");
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "电梯仍在运行，不能修理！", "警告", JOptionPane.WARNING_MESSAGE);
                    }
                }
                else{
                        JLabel[] label = outter.elevator[elevatorId].getElevatorPanel();
                        int currentFloor = outter.elevator[elevatorId].getCurrentFloor();
                        label[currentFloor - 1].setOpaque(false);
                        label[currentFloor - 1].setBackground(Color.WHITE);
                        outter.elevator[elevatorId].setLastCmd(1);
                        label[0].setOpaque(true);
                        label[0].setBackground(Color.BLACK);
                        outter.elevator[elevatorId].setCurrentFloor(1);
                        outter.elevator[elevatorId].setState(HElevator.RUN);
                        repairJbt.setText("修理电梯");
                }
            }
        });

        JLabel warnJlb = new JLabel("* 只有管理员可操作此按钮");
        warnJlb.setForeground(Color.RED);
        warnJlb.setBounds(500,550,200,40);
        labContainer.add(repairJbt);
        labContainer.add(warnJlb);

        //显示时间
        timeDisplay = new JLabel();
        timeDisplay.setBounds(330,30,130,30);
        new TimeDisplay(timeDisplay);
        labContainer.add(timeDisplay);

        // 显示牌
        labDisplay = new JLabel("    显   示   牌");
        labDisplay.setBounds(350,80,90,30);
        labDisplay.setBorder(BorderFactory.createLineBorder(Color.black));
        labContainer.add(labDisplay);

        // 显示当前所在层数
        labFloor = new JLabel();
        labFloor.setBounds(350,110,90,50);
        labFloor.setBorder(BorderFactory.createLineBorder(Color.black));
        labContainer.add(labFloor);

        // 显示当前电梯内人数
        labCountPeople = new JLabel();
        labCountPeople.setBounds(350,160,90,50);
        labCountPeople.setBorder(BorderFactory.createLineBorder(Color.black));
        labContainer.add(labCountPeople);

        // 电梯运行状态
        labStatus = new JLabel();
        labStatus.setBounds(350,210,90,50);
        labStatus.setBorder(BorderFactory.createLineBorder(Color.black));
        labContainer.add(labStatus);

        // 显示牌添加多线程控制
        Runnable runStatus = new DisplayRunnable(outter.elevator[elevatorId],labFloor,labCountPeople,labStatus);
        Thread thrStatus = new Thread(runStatus);
        thrStatus.start();

        lab = new JLabel("     电梯  ");
        lab.setBounds(80, 50, 70, 20);

        //显示命令
        lab = new JLabel("随机内呼命令");
        lab.setBounds(560,60,80,20);
        labContainer.add(lab);

        labCmd = new JTextArea();
        labCmd.setBounds(500,80,200,300);
        labCmd.setBorder(BorderFactory.createLineBorder(Color.black));
        labCmd.setLineWrap(true);
        labCmd.setEditable(false);
        JScrollPane jsp = new JScrollPane(labCmd);
        jsp.setBounds(500,80,200,300);
        labContainer.add(jsp);

        //产生内呼命令的按钮
        btnCmd =new JButton("产生内呼命令");
        btnCmd.setBounds(530,400,140,30);
        btnCmd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed (ActionEvent e){
               // int coutPeople = outter.elevator[elevatorId].getCountPeople();
                if((outter.elevator[elevatorId].getCountPeople()) != 0) {
                    randCommand();
                }
                else {
                    labCmd.append("电梯内没有人，无法产生内呼命令\n");
                }
            }
        });
        labContainer.add(btnCmd);
    }

   public void randCommand() {
        Random seed = new Random();
        int floor = seed.nextInt(20) + 1;   //产生随机楼层
        labCmd.append("内呼命令目标楼层： " + floor  + "层\n");

        HElevator e = outter.elevator[elevatorId];
        e.setRandomCmd(floor, 1);
        if(e.getDirection() == HElevator.PAUSED){
            e.setLastCmd(floor);    //如果电梯是空闲状态，把楼层按键的值设为最终目标楼层
        }

        if(e.getDirection() == HElevator.UP){
            e.setLastCmd(e.getMaxCmd());
        }

        if(e.getDirection() == HElevator.DOWN){
            e.setLastCmd(e.getMinCmd());
        }

    }

    public static void main(String[] args) {

    }
}



